iT邦幫忙

2024 iThome 鐵人賽

DAY 12
0
@IBOutlet var sceneView: ARSCNView!

// viewdidload
let configuration = ARBodyTrackingConfiguration()
        sceneView.session.run(configuration)
        
     // ARSCNViewDelegate   
   func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
        guard let bodyAnchor = anchor as? ARBodyAnchor else { return }
        
        // 刷新參數
        node.transform = SCNMatrix4(bodyAnchor.transform)
        
        // 遍歷人體骨架
        for jointName in bodyAnchor.skeleton.definition.jointNames {
            let jointIndex = bodyAnchor.skeleton.definition.index(for: jointName)
            let jointTransform = bodyAnchor.skeleton.modelTransform(for: jointIndex)
            let jointNode = node.childNode(withName: jointName, recursively: true) ?? createJointNode(jointName: jointName)
            
            jointNode.transform = SCNMatrix4(jointTransform)
            node.addChildNode(jointNode)
        }
    }

    // 創建節點
    private func createJointNode(jointName: ARSkeleton.JointName) -> SCNNode {
        let sphere = SCNSphere(radius: 0.02)
        sphere.firstMaterial?.diffuse.contents = UIColor.red
        let jointNode = SCNNode(geometry: sphere)
        jointNode.name = jointName.rawValue
        return jointNode
    }
}

記得加入相機的權限歐


上一篇
ARkit Face Tracking基本應用
下一篇
ARkit 圖像辨識
系列文
手機Swift性能專家30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言